import { Component, OnInit } from '@angular/core';
import {MoviesService} from '../movies.service'
import {Type} from "../type";
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router} from '@angular/router'; //导入router服务
@Component({
  selector: 'app-intrest',
  templateUrl: './interest.component.html',
  styleUrls: ['./interest.component.css']
})
export class InterestComponent implements OnInit {
  types=[];
  chooseTypes=new Set<string>();
  constructor(private moviesService:MoviesService,private message: NzMessageService,private router :Router) { }
  getTypes(){
    this.moviesService.getTypes().subscribe(res=>{
      console.log("电影类型：",res);
      if(res.code==0){
        this.types=res.data;
      }else{
        this.message.create('error',res.msg);
      }
    })
  }
  //用户选择偏好
  add(type:string){
      console.log("选择：",type);
      if(this.chooseTypes.has(type)){
        this.chooseTypes.delete(type);
      }else{
        this.chooseTypes.add(type);
      }
      console.log('一共选择了：',this.chooseTypes);
  }
  //保存偏好
  save(){
    const username=window.localStorage['username'];
    // for(let i in this.chooseTypes){
    //   console.log(i);
    //   fd.append('types',i)
    // }

    this.moviesService.saveTypes(this.chooseTypes,username).subscribe(res=>{
        console.log("保存偏好：",res);
        if(res.code==0){
          this.message.create('success',res.data);
          this.router.navigateByUrl('/home/movies');
        }else{
          this.message.create('error',res.msg);
        }
    })
  }
  ngOnInit(): void {
    this.getTypes()
  }

}
